package bb.lanxing.util;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import com.umeng.commonsdk.framework.UMModuleRegister;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

public class ProcessUtil {
    final static private String TAG = "ProcessUtil";
    public static boolean isServiceRunning(Context context, String str) {
        boolean z;
        Log.logv(TAG, "[ProcessUtil] isServiceRunning <<<< serviceName = " + str);
        List<ActivityManager.RunningServiceInfo> runningServices = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningServices(Integer.MAX_VALUE);
        if (runningServices.size() > 0) {
            for (ActivityManager.RunningServiceInfo runningServiceInfo : runningServices) {
                if (str.equals(runningServiceInfo.service.getClassName()) && context.getPackageName().equals(runningServiceInfo.service.getPackageName())) {
                    Log.logv(TAG, "[ProcessUtil] isServiceRunning, className = " + runningServiceInfo.service.getClassName() + ", packageName = " + runningServiceInfo.service.getPackageName());
                    z = runningServiceInfo.started;
                    break;
                }
            }
        }
        z = false;
        Log.logv(TAG, "[ProcessUtil] isServiceRunning >>>> isRunning = " + z);
        return z;
    }

    public static boolean killProcess(Context context, String str) {
        try {
            ActivityManager.RunningAppProcessInfo runningProcess = getRunningProcess(context, str);
            if (runningProcess == null) {
                return false;
            }
            Process.killProcess(runningProcess.pid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static ActivityManager.RunningAppProcessInfo getRunningProcess(Context context, String str) {
        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses()) {
            Log.i(/*UMModuleRegister.PROCESS*/TAG, "getRunningProcess: " + runningAppProcessInfo.processName);
            if (runningAppProcessInfo.processName.contains(str)) {
                return runningAppProcessInfo;
            }
        }
        return null;
    }

    public static String getProcessName(Context context) {
        Log.v(TAG, "[ProcessUtil] getProcessName <<<<");
        int myPid = Process.myPid();
        String str = null;
        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses()) {
            if (myPid == runningAppProcessInfo.pid) {
                str = runningAppProcessInfo.processName;
            }
        }
        Log.v(TAG, "[ProcessUtil] getProcessName >>>> processName = " + str);
        return str;
    }

    public static String getProcessName() {
        int myPid = Process.myPid();
        try {
            return new BufferedReader(new FileReader(new File("/proc/" + myPid + "/cmdline"))).readLine().trim();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isProcessAlive(int i) {
        return new File("/proc/" + i + "/").exists();
    }
}
